package tailrec

object Implicits {

  implicit class SqlBooleanImplicit(b: Boolean) {

    import wangzx.scala_commons.sql._

    def default(): SQLWithArgs = sql""

    // call by name
    def optional(left: => SQLWithArgs, right: => SQLWithArgs = null): SQLWithArgs =
      if (b) left
      else {
        val r = right
        if (r != null) r
        else default
      }

    def optional(f: => SQLWithArgs): SQLWithArgs = if (b) f else default

  }


  /**
    *   val query = sql " SELECT * FROM user WHERE 1=1 "
    *   val name:Option[String] = None
    *   val optionSql = name.optional( str => s" and name = ${str}")
    */
  implicit class SqlOptionEx[T] (opt: Option[T]) {
    import wangzx.scala_commons.sql._
    def optional(op: T => SQLWithArgs) : SQLWithArgs = opt match {
      case Some(value) => op(value)
      case None => SQLWithArgs("", Seq.empty)
    }



  }
